package com.cg.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.cg.business.CGUserController;

public class CGUtilClass {
	private static Log log = LogFactory.getLog(CGUserController.class.getName());
	
	
	public void WriteXml(ArrayList<String> latLongList,String realPath,String fileName) throws  TransformerException, ParserConfigurationException{
		
		
		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
		Document document = documentBuilder.newDocument();
		Element rootElement = document.createElement("markers"); // creates a element
		int n = 0;
		while (latLongList.size()> n+1){
		
			Element element1 = document.createElement("marker"); //create another element
		
		   
			element1.setAttribute(new String("lat"), latLongList.get(n));
			n++;
			element1.setAttribute(new String("lng"), latLongList.get(n));
			
		    rootElement.appendChild(element1); // add element1 under rootElement
			 n++;	
			 
			 element1.setAttribute(new String("name"), latLongList.get(n));
			 n++;
	   }
	
	    document.appendChild(rootElement); // add the rootElement to the document
	    File file = new File( realPath, fileName ); 
//	    File file = new File( realPath+fileName ); 
	 
	 try{
	TransformerFactory transformerFactory = TransformerFactory.newInstance();

	Transformer transformer = transformerFactory.newTransformer();
	DOMSource source = new DOMSource(document);
  //  StreamResult result = new StreamResult(new File(realPath+xsl+".xml"));
    StreamResult result = new StreamResult(file);
    transformer.transform(source, result);
	}			
	catch (TransformerConfigurationException e)
	{
		e.printStackTrace();
		
	}
	catch (TransformerFactoryConfigurationError e)
	{
		e.printStackTrace();
		
	}
	catch (TransformerException e)
	{
		e.printStackTrace();
		
	}
//	 CGUtilClass utilObj = new CGUtilClass();
//	 utilObj.copyfile(realPath);
	

}
	
	
	
	
	public void copyfile(String path){
		  try{
		  File f1 = new File("C:/Location.xml");
		  File f2 = new File( path, "\\finalLocation.xml" ); 
		  
		  log.debug("f2"+f2);
		  log.debug(f2.getAbsolutePath());
		  InputStream in = new FileInputStream(f1);
		  
		  //For Append the file.
		//  OutputStream out = new FileOutputStream(f2,true);

		  //For Overwrite the file.
		  OutputStream out = new FileOutputStream(f2);

		  byte[] buf = new byte[1024];
		  int len;
		  while ((len = in.read(buf)) > 0){
		  out.write(buf, 0, len);
		  }
		  in.close();
		  out.close();
		  log.debug("File copied.");
		  }
		  catch(FileNotFoundException ex){
			  log.error(ex.getMessage() + " in the specified directory.");
		  System.exit(0);
		  }
		  catch(IOException e){
			  log.error(e.getMessage());  
		  }
	}

}
